#include "stdafx.h"

#include "MainMenuGUIController.h"

using namespace std;

MainMenuGUIController::MainMenuGUIController(Model* model, Ogre::RenderWindow* window, string scheme, string layout)
	: GUIController(model, window, scheme),
	m_wmgr(CEGUI::WindowManager::getSingleton()) {
	m_GUIMade = false;

	m_newScreen = SCREEN_MAIN;
	m_oldScreen = m_newScreen;

	// Set the default mouse cursor
	CEGUI::System::getSingleton().setDefaultMouseCursor("OgreTrayImages", "MouseArrow");

	makeGUILayout();
}

MainMenuGUIController::~MainMenuGUIController () {
	if(m_sheet) {
		CEGUI::WindowManager::getSingleton().destroyWindow(m_sheet);
	}
}

// Remove this if we decide to not actually update anything
bool MainMenuGUIController::update(float deltaTime) {
	m_GUIMade = true;

	// Update the menu if there is change
	if (m_newScreen != m_oldScreen) {
		if(m_sheet) {
			CEGUI::WindowManager::getSingleton().destroyWindow(m_sheet);
		}
		MainMenuGUIController::makeGUILayout();
	}

	return GUIController::update(deltaTime);
}

void MainMenuGUIController::makeGUILayout() {	
     switch (m_newScreen)
    {
        case SCREEN_MAIN:
            makeGUIMain();
            break;
        case SCREEN_CREDITS:
            makeGUICredits();
            break;
        case SCREEN_HELP:
            makeGUIHelp();
            break;
		default:
            makeGUIMain();
            break;
	 }
	 

	m_oldScreen = m_newScreen;
}

void MainMenuGUIController::makeGUIMain() {
	CEGUI::Window *m_sheet = m_wmgr.createWindow("DefaultWindow", "MainMenuGUI/Sheet");

	m_quit = m_wmgr.createWindow("OgreTray/Button", "MainMenuGUI/QuitButton");
	m_quit->setText("Quit");
	m_quit->setSize(CEGUI::UVector2(CEGUI::UDim(0.15f, 0), CEGUI::UDim(0.05f, 0)));
	m_quit->setHorizontalAlignment(CEGUI::HA_RIGHT);
	m_quit->setVerticalAlignment(CEGUI::VA_BOTTOM);
	m_sheet->addChildWindow(m_quit);
	m_quit->subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&MainMenuGUIController::quitGame, this));

	m_start = m_wmgr.createWindow("OgreTray/Button", "MainMenuGUI/StartButton");
	m_start->setText("Start");
	m_start->setSize(CEGUI::UVector2(CEGUI::UDim(0.15f, 0), CEGUI::UDim(0.05f, 0)));
	m_start->setHorizontalAlignment(CEGUI::HA_LEFT);
	m_start->setVerticalAlignment(CEGUI::VA_BOTTOM);
	m_sheet->addChildWindow(m_start);
	m_start->subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&MainMenuGUIController::startGame, this));

	m_replay = m_wmgr.createWindow("OgreTray/Button", "MainMenuGUI/ReplayButton");
	m_replay->setText("Replay");
	m_replay->setSize(CEGUI::UVector2(CEGUI::UDim(0.15f, 0), CEGUI::UDim(0.05f, 0)));
	m_replay->setHorizontalAlignment(CEGUI::HA_CENTRE);
	m_replay->setVerticalAlignment(CEGUI::VA_BOTTOM);
	m_sheet->addChildWindow(m_replay);
	m_replay->subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&MainMenuGUIController::replayGame, this));

	m_addressBox = m_wmgr.createWindow("OgreTray/Editbox", "MainMenuGUI/AdressBox");
	m_addressBox->setText("127.0.0.1");
	m_addressBox->setSize(CEGUI::UVector2(CEGUI::UDim(0.20f, 0), CEGUI::UDim(0.07f, 0)));
	m_addressBox->setHorizontalAlignment(CEGUI::HA_CENTRE);
	m_addressBox->setVerticalAlignment(CEGUI::VA_CENTRE);
	m_sheet->addChildWindow(m_addressBox);
	//addressBox->subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&MainMenuGUIController::replayGame, this));

	CEGUI::System::getSingleton().setGUISheet(m_sheet);
}

void MainMenuGUIController::makeGUICredits() {
	
}

void MainMenuGUIController::makeGUIHelp() {
	
}

bool MainMenuGUIController::quitGame(const CEGUI::EventArgs &e) {
	StateManager* m_stateManager;
	m_stateManager = StateManager::getInstance();
	m_stateManager->quitGame();
	return true;
}

bool MainMenuGUIController::startGame(const CEGUI::EventArgs &e) {
	StateManager* m_stateManager;
	m_stateManager = StateManager::getInstance();
	// Some conversions between CEGUIs string type and const char pointer
	// See  http://www.cegui.org.uk/phpBB2/viewtopic.php?p=408
	CEGUI::String ipCEGUIString = m_addressBox->getText();
	std::string ipString = ipCEGUIString.c_str();
	m_stateManager->startGameStateFlagged(ipString);
	return true;
}

bool MainMenuGUIController::replayGame(const CEGUI::EventArgs &e) {
	StateManager* m_stateManager;
	m_stateManager = StateManager::getInstance();
	m_stateManager->startReplayState();
	return true;
}